package com.gitee.huanminabc.utils_tools.config_refresh.utils;


import org.springframework.context.ApplicationContext;
import org.springframework.context.expression.BeanFactoryAccessor;
import org.springframework.expression.spel.support.StandardEvaluationContext;
import org.yaml.snakeyaml.Yaml;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.util.List;
import java.util.Map;
import java.util.Properties;

/**
 * 将YAML文件转换为Properties文件
 */
public class YamlToPropertiesConverter {


    public static Properties convertYamlToProperties(File yamlFile) {
        Yaml yaml = new Yaml();
        try (InputStream inputStream = Files.newInputStream(yamlFile.toPath())) {
            // 解析YAML文件为Map  
            Map<String, Object> yamlMap = yaml.load(inputStream);

            // 创建Properties对象
            Properties properties = new Properties();
            if (yamlMap == null || yamlMap.isEmpty()) {
                return properties;
            }
            // 递归地将Map转换为properties格式
            flattenMap(yamlMap, "", properties);
            return properties;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static Properties convertYamlToPropertiesSpring(File yamlFile, ApplicationContext context) {
        // 将ioc容器设置到上下文中
        StandardEvaluationContext standardEvaluationContext = new StandardEvaluationContext(context);
        standardEvaluationContext.addPropertyAccessor(new BeanFactoryAccessor());
        Properties properties = convertYamlToProperties(yamlFile);

        //转换所有的值表达式
        properties.forEach((k, v) -> {
            String value = v.toString();
            String result = SpELUtil.spElParser(value, String.class, context);
            properties.put(k, result);
        });
        return properties;
    }


    //写入properties文件
    public static void propertiesWriteFile(File propertiesFile, Properties properties) {
        // 将Properties写入文件
        try (FileWriter writer = new FileWriter(propertiesFile)) {
            properties.store(writer, null);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    private static void flattenMap(Map<String, Object> map, String prefix, Properties properties) {
        for (Map.Entry<String, Object> entry : map.entrySet()) {
            String key = prefix + (prefix.isEmpty() ? "" : ".") + entry.getKey();
            Object value = entry.getValue();

            if (value instanceof Map) {
                flattenMap((Map) value, key, properties);
            } else if (value instanceof List) {
                // 处理List，这里需要决定如何将其转换为properties格式，因为properties不支持List  
                // 你可以将其转换为索引形式的key，例如：list.0, list.1, ...  
                // 或者转换为逗号分隔的字符串，取决于你的需求  
                // 此处示例略过List的处理  
            } else {
                properties.put(key, value.toString());
            }
        }
    }
}
